Telegram Group & Telegram Channel
👍 Команда дня: contextlib.suppress

Сегодня разберём полезную команду для работы с contextlib.suppress — удобный способ игнорировать определённые исключения без громоздкого try-except.

from contextlib import suppress

# Игнорируем FileNotFoundError при удалении файла
with suppress(FileNotFoundError):
import os
os.remove("non_existent_file.txt")


Зачем это нужно:
Упрощает код, когда вы хотите молча пропустить определённые ошибки.
Например, удаление файла, который может не существовать, или обработка данных, где некоторые ключи могут отсутствовать.

Пример из реальной жизни:
data = {"name": "Alice"}
with suppress(KeyError):
print(data["age"]) # Не сломается, даже если ключа "age" нет


Библиотека питониста #буст
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/pyproglib/6695
Create:
Last Update:

👍 Команда дня: contextlib.suppress

Сегодня разберём полезную команду для работы с contextlib.suppress — удобный способ игнорировать определённые исключения без громоздкого try-except.

from contextlib import suppress

# Игнорируем FileNotFoundError при удалении файла
with suppress(FileNotFoundError):
import os
os.remove("non_existent_file.txt")


Зачем это нужно:
Упрощает код, когда вы хотите молча пропустить определённые ошибки.
Например, удаление файла, который может не существовать, или обработка данных, где некоторые ключи могут отсутствовать.

Пример из реальной жизни:
data = {"name": "Alice"}
with suppress(KeyError):
print(data["age"]) # Не сломается, даже если ключа "age" нет


Библиотека питониста #буст

BY Библиотека питониста | Python, Django, Flask


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/pyproglib/6695

View MORE
Open in Telegram


Библиотека питониста | Python Django Flask Telegram | DID YOU KNOW?

Date: |

The Singapore stock market has alternated between positive and negative finishes through the last five trading days since the end of the two-day winning streak in which it had added more than a dozen points or 0.4 percent. The Straits Times Index now sits just above the 3,060-point plateau and it's likely to see a narrow trading range on Monday.

The global forecast for the Asian markets is murky following recent volatility, with crude oil prices providing support in what has been an otherwise tough month. The European markets were down and the U.S. bourses were mixed and flat and the Asian markets figure to split the difference.The TSE finished modestly lower on Friday following losses from the financial shares and property stocks.For the day, the index sank 15.09 points or 0.49 percent to finish at 3,061.35 after trading between 3,057.84 and 3,089.78. Volume was 1.39 billion shares worth 1.30 billion Singapore dollars. There were 285 decliners and 184 gainers.

Библиотека питониста | Python Django Flask from us


Telegram Библиотека питониста | Python, Django, Flask
FROM USA